package com.itheima.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        userService = new UserServiceImpl();
        List<User> users = userService.findAll();
        // 转json格式字符串
        ObjectMapper objectMapper = new ObjectMapper();
        // 日期格式
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String userJ = objectMapper.writeValueAsString(users);
        // 传到vue层
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(userJ);
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // post 读取json格式封装成对象
        BufferedReader reqReader = req.getReader();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(reqReader, User.class);

        userService.save(user);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // get 带来的数据
        String idStr = req.getParameter("id");
        int id = Integer.parseInt(idStr);

        User user = userService.findById(id);

        // 对象 转json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        // 日期格式
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String userJ = objectMapper.writeValueAsString(user);
        // 传走
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(userJ);
    }
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(reader, User.class);

        userService.edit(user);
    }

    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收get传递的 ids 数组字符串
        String ids = req.getParameter("ids");
        System.out.println(ids);
        String[] idStr = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id: idStr) {
            idList.add(Integer.parseInt(id));
        }
        // 传到service
        userService.deleteByIds(idList);
    }
}
